﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.EntityClient;
using UniversalDataAccess.BusinessEntities;

namespace UniversalDataAccess
{
    public class OrganizationalDivisionsDataAccess : DivisionsDataAccess
    {
        public OrganizationalDivisionsDataAccess(string dbName, string conn, bool usePredefinedMetadata)
            :base(dbName, conn, usePredefinedMetadata)
        {
        }

        public List<OrganizationalDivision> SelectOrganizationalDivisionsList()
        {
            try
            {
                //Select Division List
                List<Division> divList = SelectDivisionsList();

                //Select Organizational Divisions List
                List<ORGANIZATIONAL_DIVISION> orgDivList = dataContext.ORGANIZATIONAL_DIVISION.ToList();

                //Merge result
                var res = (from div in divList
                           join orgDiv in orgDivList on div.ID equals orgDiv.ID
                           select new OrganizationalDivision
                          {
                              DataBaseName = dataBaseName,
                              CLASS_ID = div.CLASS_ID,
                              EXT_ID = div.EXT_ID,
                              ID = div.ID,
                              NAME = div.NAME,
                              PARENT_ID = div.PARENT_ID,
                              BOSS_ID = div.BOSS_ID,
                              DATE_CREATED = div.DATE_CREATED,
                              DESCRIPTION = div.DESCRIPTION,
                              EMAIL = orgDiv.EMAIL,
                              SLOGAN = orgDiv.SLOGAN
                          }).ToList();

                return res;
            }
            catch
            {
                throw new Exception("Error while SelectOrganizationalDivisionBy_Id");
            }
        }

        public OrganizationalDivision SelectOrganizationalDivisionBy_Id(int Id)
        {
            try
            {
                //Select Tobject part by Id
                Division divisionPart = SelectDivisionBy_Id(Id);

                //Select Division part
                List<ORGANIZATIONAL_DIVISION> orgDivisionPart = dataContext.ORGANIZATIONAL_DIVISION.ToList();

                //Merge result
                var res = (from orgDiv in orgDivisionPart
                           where orgDiv.ID == divisionPart.ID
                           select new OrganizationalDivision
                           {
                               DataBaseName = dataBaseName,
                               CLASS_ID = divisionPart.CLASS_ID,
                               EXT_ID = divisionPart.EXT_ID,
                               ID = divisionPart.ID,
                               NAME = divisionPart.NAME,
                               PARENT_ID = divisionPart.PARENT_ID,
                               BOSS_ID = divisionPart.BOSS_ID,
                               DATE_CREATED = divisionPart.DATE_CREATED,
                               DESCRIPTION = divisionPart.DESCRIPTION,
                               EMAIL = orgDiv.EMAIL,
                               SLOGAN = orgDiv.SLOGAN
                           }).FirstOrDefault();

                return res;
            }
            catch
            {
                throw new Exception("Error while SelectOrganizationalDivisionBy_Id");
            }
        }

        public void InsertOrganizationalDivision(OrganizationalDivision organizationalDivision, ref int generated_Id)
        {
            try
            {
                //Insert the Division part
                InsertDivision(organizationalDivision, ref generated_Id);

                //Insert the OrganizationalDivision part
                int new_Id = generated_Id;
                ORGANIZATIONAL_DIVISION organizationalDivisionPart = new ORGANIZATIONAL_DIVISION();
                organizationalDivisionPart.DIVISION = dataContext.DIVISION.Where(t => t.ID == new_Id).FirstOrDefault();
                organizationalDivisionPart.ID = new_Id;
                organizationalDivisionPart.SLOGAN = organizationalDivision.SLOGAN;
                organizationalDivisionPart.EMAIL = organizationalDivision.EMAIL;
               
                //Submit changes
                dataContext.AddToORGANIZATIONAL_DIVISION(organizationalDivisionPart);
                dataContext.SaveChanges();
            }
            catch(Exception ex)
            {
                throw new Exception("Error while InsertOrganizationalDivision");
            }
        }

        public void UpdateOrganizationalDivision(OrganizationalDivision organizationalDivision)
        {
            try
            {
                //Update the Division part
                UpdateDivision(organizationalDivision);

                //Update the OrganizationalDivision part
                ORGANIZATIONAL_DIVISION organizationalDivisionPart = dataContext.ORGANIZATIONAL_DIVISION.Where(t => t.ID == organizationalDivision.ID).FirstOrDefault();
                organizationalDivisionPart.SLOGAN = organizationalDivision.SLOGAN;
                organizationalDivisionPart.EMAIL = organizationalDivision.EMAIL;
                               
                //Submit changes
                dataContext.SaveChanges();
            }
            catch(Exception ex)
            {
                throw new Exception("Error while UpdateOrganizationalDivision");
            }
        }

        public void DeleteOrganizationalDivision(OrganizationalDivision organizationalDivision)
        {
            try
            {
                //Delete the OrganizationalDivision part
                ORGANIZATIONAL_DIVISION divPart = dataContext.ORGANIZATIONAL_DIVISION.Where(t => t.ID == organizationalDivision.ID).FirstOrDefault();

                dataContext.DeleteObject(divPart);
                dataContext.SaveChanges();

                //Delete the Division part
                DeleteDivision(organizationalDivision);
            }
            catch (Exception ex)
            {
                throw new Exception("Error while DeleteOrganizationalDivision");
            }
        }
    }
}
